package com.lingxu;

import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.util.zip.DataFormatException;

/**
 * @program: dataanalysis
 * @ClassName DateUtils
 * @description:
 * @author: gxy
 * @create: 2023-04-24 15:57
 * @Version 1.0
 **/
@Slf4j
public class DateUtils {

    private final static String yyyy_MM_dd_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    private final static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
    private final static String yyyyMMddHHmm = "yyyyMMddHHmm";
    private final static String yyyyMMdd = "yyyyMMdd";

    private final static String yyyyMMdd_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";

    private final static List<String> dateFormatList = new ArrayList<String>(){{
        add(yyyy_MM_dd_HH_MM_SS);
        add("yyyy-MM-dd");
        add(yyyyMMddHHmmss);
        add(yyyyMMddHHmm);
        add(yyyyMMdd);
        add(yyyyMMdd_HH_MM_SS);
        add("yyyy.MM.dd HH:mm:ss");
        add("yyyy.MM.dd HH:mm");
        add("yyyy.MM.dd");
        add("yyyy/M/d HH:mm:ss");
        add("yyyy/M/d HH:mm");
    }};


    public static String buildDate(String date,String formate) {
        if(null==date||date.trim().equals("")) {
            return null;
        }
        boolean isSuccess = Boolean.FALSE;
        Date data = null;
        if(date.matches("^\\d{8} \\d{6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMdd HHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{14,18}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                data = df.parse(date.substring(0,14));
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
//        2025/01/0318:40:05
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}\\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/ddHH:mm:ss");
            try {
                data = df.parse(date);
                return new SimpleDateFormat(formate).format(data);
            } catch (ParseException e) {
            }
        }
//        if(date.matches("^\\d{15}$")&&!isSuccess) {
//            DateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
//            try {
//                data = df.parse(date);
//                isSuccess = true;
//            } catch (ParseException e) {
//            }
//        }
//        if(date.matches("^\\d{17}$")&&!isSuccess) {
//            DateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
//            try {
//                data = df.parse(date);
//                isSuccess = true;
//            } catch (ParseException e) {
//            }
//        }
        if(date.matches("^\\d{8} \\d{0,2}:\\d{0,2}:\\d{0,2}:\\d{0,4}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2} \\d{6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/dd HHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}\\d{0,2}\\d{0,2} \\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2} \\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}\\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2} \\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}\\d{6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/ddHHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}\\d{0,2}.\\d{0,2}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/ddHH.mm.ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}/\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
//        yyyy-MM-dd-HH.mm.ss.nnnnnn
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}-\\d{0,2}.\\d{0,2}.\\d{0,2}.\\d{0,6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        //        yyyy-MM-dd-HH.mm.ss.nnnnnn
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}/\\d{0,2}.\\d{0,2}.\\d{0,2}.\\d{0,6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/dd/HH.mm.ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}-\\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy.MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}.\\d{0,2}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy.MM.dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{8}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMdd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        String res;
        if(isSuccess) {
            res=new SimpleDateFormat(formate).format(data);
        } else {
            res = date;
        }
        return res;
    }

    public static Date toDate(String date) {
        if(null==date||date.trim().equals("")) {
            return null;
        }
        boolean isSuccess = Boolean.FALSE;
        Date data = null;

        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2} \\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}\\d{0,2}\\d{0,2}\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-ddHHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}\\d{0,2}\\d{0,2}\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/ddHHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2} \\d{0,2}.\\d{0,2}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}-\\d{0,2}.\\d{0,2}.\\d{0,2}.\\d{0,6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}/\\d{0,2} \\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM HH:mm:ss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{4}/\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}/\\d{0,2}/\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy/MM/dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}-\\d{0,2}-\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy.MM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{4}.\\d{0,2}.\\d{0,2}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyy.MM.dd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        if(date.matches("^\\d{6}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMM");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{17}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }

        if(date.matches("^\\d{8}$")&&!isSuccess) {
            DateFormat df=new SimpleDateFormat("yyyyMMdd");
            try {
                data = df.parse(date);
                isSuccess = true;
            } catch (ParseException e) {
            }
        }
        return data;
    }

    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public static String getTimes(String date) {
        if(isInteger(date)) {
            return date;
        }
        if(null==date||date.trim().equals("")) {
            return null;
        }
        boolean isSuccess = Boolean.FALSE;
        Integer hour = 0;
        Integer minute = 0;
        Integer second = 0;
        if(date.matches("^\\d{0,2}:\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            String[] timesA = date.split(":");
            hour = Integer.parseInt(timesA[0])*60*60;
            minute = Integer.parseInt(timesA[1])*60;
            second = Integer.parseInt(timesA[2]);
        } else if(date.matches("^\\d{0,2}:\\d{0,2}$")&&!isSuccess) {
            String[] timesA = date.split(":");
            minute = Integer.parseInt(timesA[0])*60;
            second = Integer.parseInt(timesA.length>=2?timesA[1]:"0");
        }

        if(date.matches("^\\d{8}$")&&!isSuccess) {
        }
        return String.valueOf(hour+minute+second);
    }


    /**
     * 对时间字符串 优先采取yyyy-MM-dd HH:mm:ss 解析
     *时间格式的不确定性 需要多次多种格式进行解析
     * @param date
     * @return
     * @throws DataFormatException
     */
    public static Date getDate(String date) throws DataFormatException {

        for (String format : dateFormatList) {
            try {
                return new SimpleDateFormat(format).parse(date);
            }catch (Exception e){
                continue;
            }
        }
        log.error("时间格式转换失败:{}",date);
        throw new DataFormatException("时间格式转换失败:"+date);
    }

    public static String getDate(Date date) throws DataFormatException {

        for (String format : dateFormatList) {
            try {
                return new SimpleDateFormat(format).format(date);
            }catch (Exception e){
                continue;
            }
        }
        log.error("时间格式转换失败:{}",date);
        throw new DataFormatException("时间格式转换失败:"+date);
    }

    public static void main(String[] args) throws DataFormatException {
//        20201225101122282
        String da = "";
        System.out.println(buildDate("2025/01/0318:40:05","yyyy-MM-dd HH:mm:ss"));
        System.out.println(DateUtils.getTimes("2分".replace("时",":").replace("分",":").replace("秒","")));
//        System.out.println(getDate("2016/4/28  9:31:00"));
//        System.out.println(StringUtils.isNotBlank(da)?da.replaceAll("\"","").trim():null);
//        System.out.println("CSV".toLowerCase(Locale.ROOT).equals("csv"));
//        System.out.println(buildDate("2022-01-0611:53:57","yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateUtils.getTimes("173".replace("时",":").replace("分",":").replace("秒","")));
//        System.out.println(toDate("2010-09-17-10.32.01.968410"));
//        System.out.println(getTimes("00:00:48"));
//        System.out.println(getTimes("00:02:58"));
//        System.out.println(getTimes("01:02:58"));
//        System.out.println(getTimes("00:48"));
//        String time = "2分49秒";
//        time = time.replace("时",":").replace("分",":").replace("秒","");
//        System.out.println(getTimes(time));
//        System.out.println(buildDate("2023/05/0312.00.02","yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateUtils.buildDate("2020/01/0616:05:43", "yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateUtils.buildDate("2003/06/09/22.00.42.295011", "yyyy-MM-dd HH:mm:ss"));
//        System.out.println(DateUtils.buildDate("2002-09-04-16.34.54.842641", "yyyy-MM-dd HH:mm:ss"));

//        System.out.println("20.09.08".replaceAll("\\.",""));
    }
}
